<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0 auto;
            width: 60%;
            cursor: pointer;
        }

        * {
            box-sizing: border-box;
            list-style: none;
        }

        .header {
            background-color: #88de87;
            padding: 30px 40px;
            border-radius: 80px 0px;
            text-align: center;
        }

        .header:after {
            content: "";
            display: table;
            clear: both;
        }

        input {
            border: none;
            width: 75%;
            padding: 10px;
            float: left;
            font-size: 16px;
        }


        .addBtn {
            padding: 10px;
            width: 25%;
            background: #d9d9d9;
            color: #555;
            float: left;
            text-align: center;
            font-size: 16px;
            cursor: pointer;
            transition: 0.3s;
        }

        .addBtn:hover {
            background-color: #bbb;
        }

        ul {
            margin: 0;
            padding: 0;
        }

        ul li {
            cursor: pointer;
            position: relative;
            padding: 12px 8px 12px 40px;
            background: #eee;
            font-size: 18px;
            transition: 0.2s;
        }

        ul li:nth-child(odd) {
            background: #f9f9f9;
        }

        ul li:hover {
            background: #ddd;
        }

        ul li.checked {
            background: #888;
            color: #fff;
            text-decoration: line-through;
        }

        ul li.checked::before {
            content: '';
            position: absolute;
            border-color: #fff;
            border-style: solid;
            border-width: 0 2px 2px 0;
            top: 10px;
            left: 16px;
            transform: rotate(45deg);
            height: 15px;
            width: 7px;
        }

        .close {
            position: absolute;
            right: 0;
            top: 0;
            padding: 12px 16px 12px 16px;
        }

        .close:hover {
            background-color: #f44336;
            color: white;
        }
        .footer{
            height: 50px;
            background-color: #88de87;

        }
    </style>
</head>

<body>
    <div class="header" id="myDIV">
        <h1 style="margin: 5px;">My to do list</h1>
        <input type="text" id="myInput" placeholder="请输入......">
        <span class="addBtn" onclick="newElement()">添加</span>
    </div>

    <ul id="myUL">
        <li class="checked">唱</li>
        <li>跳</li>
        <li>rap</li>
        <li>篮球</li>
        <li>跑步</li>
        <li>阅读</li>
    </ul>

    <div class="footer"></div>

    <script>
        //添加删除按键
        // let myNodelist = document.querySelectorAll('li')
        var myNodelist = document.getElementsByTagName("LI");
        var i;
        function fn(){
            for (i = 0; i < myNodelist.length; i++) {
                var span = document.createElement("SPAN");
                var txt = document.createTextNode("\u00D7");
                span.className = "close";
                span.appendChild(txt);
                myNodelist[i].appendChild(span);
            }
        }
        fn()

        //添加删除功能
        // var close = document.querySelector('.close')
        var close = document.getElementsByClassName("close");
        for (i = 0; i < close.length; i++) {
            close[i].onclick = function () {
                var div = this.parentElement;
                div.style.display = "none";
            }
        }

        //点选标记为完成
        var list = document.querySelector('ul');
        list.addEventListener('click', function (e) {
            if (e.target.tagName === 'LI') {
                e.target.classList.toggle('checked');
            }
        }, false);

        //添加待办事件
        function newElement() {
            var li = document.createElement("li");
            var input = document.getElementById("myInput");
            var inputValue = document.getElementById("myInput").value;
            var t = document.createTextNode(inputValue);
            li.appendChild(t);
            if (inputValue === '') {
                alert("此处不能为空!");
            } else {
                document.getElementById("myUL").appendChild(li);
            }
            document.getElementById("myInput").value = "";
            //————————————————————————————————————————
            fn()
            // var span = document.createElement("SPAN");
            // var txt = document.createTextNode("\u00D7");
            // span.className = "close";
            // span.appendChild(txt);
            // li.appendChild(span);

            // for (i = 0; i < close.length; i++) {
            //     close[i].onclick = function () {
            //         var div = this.parentElement;
            //         div.style.display = "none";
            //     }
            // }
            //————————————————————————————————————————
        }

        //enter键输出
        var input = document.getElementById("myInput")
        input.addEventListener('keyup', function (e) {
                if (e.key === 'Enter') {
                    newElement()
                }
            })
    </script>
</body>

</html>